package com.hxx.controller;

import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.model.StreamingResponseHandler;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StreamingAiControllerModel {

    @Autowired
    private StreamingChatLanguageModel streamingChatLanguageModel;

    @GetMapping("/streaming1")
    public String streaming1(String prompt) throws Exception{
        //创建数组
        String[] message = {""};
        streamingChatLanguageModel.generate(prompt, new StreamingResponseHandler<AiMessage>() {
            @Override
            public void onNext(String token) {
                message[0] = token;
            }
            @Override
            public void onError(Throwable throwable) {

            }
        });
        Thread.sleep(1000);
        return message[0] ;
    }
}
































